package com.sgb.kjwl.view.ui.adapter.chat;

import android.content.Context;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemNoticeViewBinding;
import com.sgb.kjwl.model.entity.chat.NewsNoticeContent2Entity;
import com.sgb.kjwl.model.entity.chat.NewsNoticeContentEntity;
import com.sgb.kjwl.model.entity.chat.NewsNoticeEntity;
import com.sgb.kjwl.view.ui.activity.WebViewX5HostActivity;
import com.sgb.kjwl.view.ui.adapter.map.BaseRecycleAdapter;
import com.sgb.kjwl.view.ui.adapter.map.ItemViewHolder;
import com.swgk.core.util.TimeUtils;

import java.util.List;

public class NewsNoticeTypeAdapter extends BaseRecycleAdapter<NewsNoticeEntity> {

    public NewsNoticeTypeAdapter(Context context, List<NewsNoticeEntity> data) {
        super(context, R.layout.item_notice_view, data);
    }

    @Override
    public void convert(ItemViewHolder holder, NewsNoticeEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        ItemNoticeViewBinding binding = (ItemNoticeViewBinding) holder.getmBinding();
        binding.tvTime.setText(TimeUtils.getIMTimeShow(entity.getTimestamp()));
        binding.noticeTitle.setText(entity.getNoticeTitle());
        if (entity.getModuleType().equals("order")) {
            binding.ivAvatar.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_notice_order));
        } else if (entity.getModuleType().equals("enquiry_price")) {
            binding.ivAvatar.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_notice_price));
        } else if (entity.getModuleType().equals("letter_inquiries")) {
            binding.ivAvatar.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_notice_quiries));
        } else if (entity.getModuleType().equals("invite")) {
            binding.ivAvatar.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_notice_invite));
        } else {
            binding.ivAvatar.setImageDrawable(ContextCompat.getDrawable(context, R.mipmap.icon_notice));
        }
        Gson gson = new Gson();
        if ("temp2".equals(entity.getTemplateType())) {
            binding.contentRecycler.setVisibility(View.GONE);
            binding.tvNoticeContent.setVisibility(View.VISIBLE);
            NewsNoticeContent2Entity content = gson.fromJson(entity.getText(), new TypeToken<NewsNoticeContent2Entity>() {
            }.getType());
            binding.tvNoticeTitle.setText(content.getTitle());
            binding.tvNoticeContent.setText(content.getContent());
            binding.tvLookDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    WebViewX5HostActivity.start(context, content.getAppUrl());
                }
            });
        } else {
            binding.tvNoticeContent.setVisibility(View.GONE);
            binding.contentRecycler.setVisibility(View.VISIBLE);
            NewsNoticeContentEntity content = gson.fromJson(entity.getText(), new TypeToken<NewsNoticeContentEntity>() {
            }.getType());
            binding.contentRecycler.setLayoutManager(new LinearLayoutManager(context));
            ContentNoticeAdapter adapter = new ContentNoticeAdapter(context, content.getContent());
            binding.contentRecycler.setAdapter(adapter);
            binding.tvNoticeTitle.setText(content.getTitle());
            binding.tvLookDetail.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    WebViewX5HostActivity.start(context, content.getAppUrl());
                }
            });
        }
    }
}
